home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1-sources / sources / orig-emacs-src / callproc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-23  |  12.8 KB  |  489 lines  |  [TEXT/EMAC]

  1. /* Synchronous subprocess invocation for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* This must precede sys/signal.h on certain machines.  */
  22. #include "sys/types.h" /* CHANGED FOR MAC */
  23. #include "signal.h" /* CHANGED FOR MAC */
  24. #include "vfork.h" /* CHANGED FOR MAC */
  25.  
  26. #include "config.h"
  27.  
  28. #define PRIO_PROCESS 0
  29. #include "sys/file.h" /* CHANGED FOR MAC */
  30. #ifdef USG5
  31. #include <fcntl.h>
  32. #endif
  33.  
  34. #ifndef O_RDONLY
  35. #define O_RDONLY 0
  36. #endif
  37.  
  38. #ifndef O_WRONLY
  39. #define O_WRONLY 1
  40. #endif
  41.  
  42. #include "lisp.h"
  43. #include "commands.h"
  44. #include "buffer.h"
  45. #include "paths.h"
  46.  
  47. #define max(a, b) ((a) > (b) ? (a) : (b))
  48.  
  49. Lisp_Object Vexec_path, Vexec_directory;
  50.  
  51. Lisp_Object Vshell_file_name;
  52.  
  53. #ifndef MAINTAIN_ENVIRONMENT
  54. /* List of strings to append to front of environment of
  55.    all subprocesses when they are started.  */
  56.  
  57. Lisp_Object Vprocess_environment;
  58. #endif
  59.  
  60. #ifdef BSD4_1
  61. /* Set nonzero when a synchronous subprocess is made,
  62.    and set to zero again when it is observed to die.
  63.    We wait for this to be zero in order to wait for termination.  */
  64. int synch_process_pid;
  65. #endif /* BSD4_1 */
  66.  
  67. /* True iff we are about to fork off a synchronous process or if we
  68.    are waiting for it.  */
  69. int synch_process_alive;
  70.  
  71. /* Nonzero => this is a string explaining death of synchronous subprocess.  */
  72. char *synch_process_death;
  73.  
  74. /* Exit code of synchronous subprocess if positive,
  75.    minus the signal number if negative.  */
  76. int synch_process_retcode;
  77.  
  78. Lisp_Object
  79. call_process_cleanup (fdpid)
  80.      Lisp_Object fdpid;
  81. {
  82.   register Lisp_Object fd, pid;
  83.   fd = Fcar (fdpid);
  84.   pid = Fcdr (fdpid);
  85.   close (XFASTINT (fd));
  86.   kill (XFASTINT (pid), SIGKILL);
  87.   return Qnil;
  88. }
  89.  
  90. #ifdef VMS
  91. #ifdef __GNUC__
  92. #define    environ $$PsectAttributes_NOSHR$$environ
  93. extern char **environ;
  94. #else
  95. extern noshare char **environ;
  96. #endif
  97. #else
  98. extern char **environ;
  99. #endif
  100.  
  101. DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
  102.   "Call PROGRAM in separate process.\012\
  103. Program's input comes from file INFILE (nil means /dev/null).\012\
  104. Insert output in BUFFER before point; t means current buffer;\012\
  105.  nil for BUFFER means discard it; 0 means discard and don't wait.\012\
  106. Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\012\
  107. Remaining arguments are strings passed as command arguments to PROGRAM.\012\
  108. Returns nil if BUFFER is 0; otherwise waits for PROGRAM to terminate\012\
  109. and returns a numeric exit status or a signal description string.\012\
  110. If you quit, the process is killed with SIGKILL.")
  111.   (nargs, args)
  112.      int nargs;
  113.      register Lisp_Object *args;
  114. {
  115.   Lisp_Object display, buffer, path;
  116.   int fd[2];
  117.   int filefd;
  118.   register int pid;
  119.   char buf[1024];
  120.   int count = specpdl_ptr - specpdl;
  121.   register unsigned char **new_argv
  122.     = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
  123.   struct buffer *old = current_buffer;
  124.  
  125.   CHECK_STRING (args[0], 0);
  126.  
  127.   if (nargs <= 1 || NULL (args[1]))
  128. #ifdef VMS
  129.     args[1] = build_string ("NLA0:");
  130. #else
  131.     args[1] = build_string ("/dev/null");
  132. #endif /* not VMS */
  133.   else
  134.     args[1] = Fexpand_file_name (args[1], current_buffer->directory);
  135.  
  136.   CHECK_STRING (args[1], 1);
  137.  
  138.   {
  139.     register Lisp_Object tem;
  140.     buffer = tem = args[2];
  141.     if (nargs <= 2)
  142.       buffer = Qnil;
  143.     else if (!(EQ (tem, Qnil) || EQ (tem, Qt)
  144.            || XFASTINT (tem) == 0))
  145.       {
  146.     buffer = Fget_buffer (tem);
  147.     CHECK_BUFFER (buffer, 2);
  148.       }
  149.   }
  150.  
  151.   display = nargs > 3 ? args[3] : Qnil;
  152.  
  153.   {
  154.     register int i;
  155.     for (i = 4; i < nargs; i++)
  156.       {
  157.     CHECK_STRING (args[i], i);
  158.     new_argv[i - 3] = XSTRING (args[i])->data;
  159.       }
  160.     /* Program name is first command arg */
  161.     new_argv[0] = XSTRING (args[0])->data;
  162.     new_argv[i - 3] = 0;
  163.   }
  164.  
  165.   filefd = open (XSTRING (args[1])->data, O_RDONLY, 0);
  166.   if (filefd < 0)
  167.     {
  168.       report_file_error ("Opening process input file", Fcons (args[1], Qnil));
  169.     }
  170.   /* Search for program; barf if not found.  */
  171.   openp (Vexec_path, args[0], "", &path, 1);
  172.   if (NULL (path))
  173.     {
  174.       close (filefd);
  175.       report_file_error ("Searching for program", Fcons (args[0], Qnil));
  176.     }
  177.   new_argv[0] = XSTRING (path)->data;
  178.  
  179.   if (XTYPE (buffer) == Lisp_Int)
  180. #ifdef VMS
  181.     fd[1] = open ("NLA0:", 0), fd[0] = -1;
  182. #else
  183.     fd[1] = open ("/dev/null", O_WRONLY), fd[0] = -1;
  184. #endif /* not VMS */
  185.   else
  186.     {
  187.       pipe (fd);
  188. #if 0
  189.       /* Replaced by close_process_descs */
  190.       set_exclusive_use (fd[0]);
  191. #endif
  192.     }
  193.  
  194.   synch_process_death = 0;
  195.   synch_process_retcode = 0;
  196.  
  197.   {
  198.     /* child_setup must clobber environ in systems with true vfork.
  199.        Protect it from permanent change.  */
  200.     register char **save_environ = environ;
  201.     register int fd1 = fd[1];
  202.     char **env;
  203.  
  204. #ifdef MAINTAIN_ENVIRONMENT
  205.     env = (char **) alloca (size_of_current_environ ());
  206.     get_current_environ (env);
  207. #else
  208.     env = environ;
  209. #endif /* MAINTAIN_ENVIRONMENT */
  210.  
  211.     pid = vfork ();
  212. #ifdef BSD4_1
  213.     /* cause SIGCHLD interrupts to look for this pid. */
  214.     synch_process_pid = pid;
  215. #endif /* BSD4_1 */
  216.  
  217.     if (pid == 0)
  218.       {
  219.     if (fd[0] >= 0)
  220.       close (fd[0]);
  221. #ifdef USG
  222. #ifdef HAVE_PTYS
  223.     setpgrp ();
  224. #endif
  225. #endif
  226.     child_setup (filefd, fd1, fd1, new_argv, env);
  227.       }
  228.  
  229.     environ = save_environ;
  230.  
  231.     close (filefd);
  232.     close (fd1);
  233.   }
  234.  
  235.   if (pid < 0)
  236.     {
  237.       close (fd[0]);
  238.       report_file_error ("Doing vfork", Qnil);
  239.     }
  240.  
  241.   if (XTYPE (buffer) == Lisp_Int)
  242.     {
  243. #ifndef subprocesses
  244.       wait_without_blocking ();
  245. #endif subprocesses
  246.       return Qnil;
  247.     }
  248.  
  249.   record_unwind_protect (call_process_cleanup,
  250.              Fcons (make_number (fd[0]), make_number (pid)));
  251.  
  252.  
  253.   if (XTYPE (buffer) == Lisp_Buffer)
  254.     Fset_buffer (buffer);
  255.  
  256.   immediate_quit = 1;
  257.   QUIT;
  258.  
  259.   {
  260.     register int nread;
  261.  
  262.     while ((nread = read (fd[0], buf, sizeof buf)) > 0)
  263.       {
  264.     immediate_quit = 0;
  265.     if (!NULL (buffer))
  266.       insert (buf, nread);
  267.     if (!NULL (display) && FROM_KBD)
  268.       redisplay_preserve_echo_area ();
  269.     immediate_quit = 1;
  270.     QUIT;
  271.       }
  272.   }
  273.  
  274.   /* Wait for it to terminate, unless it already has.  */
  275.   wait_for_termination (pid);
  276.  
  277.   immediate_quit = 0;
  278.  
  279.   set_buffer_internal (old);
  280.  
  281.   unbind_to (count);
  282.  
  283.   if (synch_process_death)
  284.     return build_string (synch_process_death);
  285.   return make_number (synch_process_retcode);
  286. }
  287.  
  288. DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
  289.   3, MANY, 0,
  290.   "Send text from START to END to a process running PROGRAM.\012\
  291. Delete the text if DELETE is non-nil.\012\
  292. Insert output in BUFFER before point; t means current buffer;\012\
  293.  nil for BUFFER means discard it; 0 means discard and don't wait.\012\
  294. Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\012\
  295. Remaining arguments are strings passed as command arguments to PROGRAM.\012\
  296. Returns nil if BUFFER is 0; otherwise waits for PROGRAM to terminate\012\
  297. and returns a numeric exit status or a signal description string.\012\
  298. If you quit, the process is killed with SIGKILL.")
  299.   (nargs, args)
  300.      int nargs;
  301.      register Lisp_Object *args;
  302. {
  303.   register Lisp_Object filename_string, start, end, status;
  304.   char tempfile[20];
  305.  
  306.   strcpy (tempfile, "/tmp/emacsXXXXXX");
  307.   mktemp (tempfile);
  308.  
  309.   filename_string = build_string (tempfile);
  310.   start = args[0];
  311.   end = args[1];
  312.   Fwrite_region (start, end, filename_string, Qnil, Qlambda);
  313.  
  314.   if (!NULL (args[3]))
  315.     Fdelete_region (start, end);
  316.  
  317.   args[3] = filename_string;
  318.   status = Fcall_process (nargs - 2, args + 2);
  319.   unlink (tempfile);
  320.   return status;
  321. }
  322.  
  323. /* This is the last thing run in a newly forked inferior
  324.    either synchronous or asynchronous.
  325.    Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
  326.    Initialize inferior's priority, pgrp, connected dir and environment.
  327.    then exec another program based on new_argv.
  328.  
  329.    This function may change environ for the superior process.
  330.    Therefore, the superior process must save and restore the value
  331.    of environ around the vfork and the call to this function.
  332.  
  333.    ENV is the environment */
  334.  
  335. child_setup (in, out, err, new_argv, env)
  336.      int in, out, err;
  337.      register char **new_argv;
  338.      char **env;
  339. {
  340.   register int pid = getpid();
  341.  
  342.   setpriority (PRIO_PROCESS, pid, 0);
  343.  
  344. #ifdef subprocesses
  345.   /* Close Emacs's descriptors that this process should not have.  */
  346.   close_process_descs ();
  347. #endif
  348.  
  349.   /* Note that use of alloca is always safe here.  It's obvious for systems
  350.      that do not have true vfork or that have true (stack) alloca.
  351.      If using vfork and C_ALLOCA it is safe because that changes
  352.      the superior's static variables as if the superior had done alloca
  353.      and will be cleaned up in the usual way.  */
  354.  
  355.   if (XTYPE (current_buffer->directory) == Lisp_String)
  356.     {
  357.       register unsigned char *temp;
  358.       register int i;
  359.  
  360.       i = XSTRING (current_buffer->directory)->size;
  361.       temp = (unsigned char *) alloca (i + 2);
  362.       bcopy (XSTRING (current_buffer->directory)->data, temp, i);
  363.       if (temp[i - 1] != '/') temp[i++] = '/';
  364.       temp[i] = 0;
  365.       chdir (temp);
  366.     }
  367.  
  368. #ifndef MAINTAIN_ENVIRONMENT
  369.   /* Set `env' to a vector of the strings in Vprocess_environment.  */
  370.   {
  371.     register Lisp_Object tem;
  372.     register char **new_env;
  373.     register int new_length;
  374.  
  375.     new_length = 0;
  376.     for (tem = Vprocess_environment;
  377.      (XTYPE (tem) == Lisp_Cons
  378.       && XTYPE (XCONS (tem)->car) == Lisp_String);
  379.      tem = XCONS (tem)->cdr)
  380.       new_length++;
  381.  
  382.     /* new_length + 1 to include terminating 0 */
  383.     env = new_env = (char **) alloca ((new_length + 1) * sizeof (char *));
  384.  
  385.     /* Copy the env strings into new_env.  */
  386.     for (tem = Vprocess_environment;
  387.      (XTYPE (tem) == Lisp_Cons
  388.       && XTYPE (XCONS (tem)->car) == Lisp_String);
  389.      tem = XCONS (tem)->cdr)
  390.       *new_env++ = (char *) XSTRING (XCONS (tem)->car)->data;
  391.     *new_env = 0;
  392.   }
  393. #endif /* Not MAINTAIN_ENVIRONMENT */
  394.  
  395.   close (0);
  396.   close (1);
  397.   close (2);
  398.  
  399.   dup2 (in, 0);
  400.   dup2 (out, 1);
  401.   dup2 (err, 2);
  402.   close (in);
  403.   close (out);
  404.   close (err);
  405.  
  406. #ifdef USG
  407. #ifndef HAVE_PTYS
  408.   setpgrp ();            /* No arguments but equivalent in this case */
  409. #endif
  410. #else
  411.   setpgrp (pid, pid);
  412. #endif /* USG */
  413.   setpgrp_of_tty (pid);
  414.  
  415. #ifdef vipc
  416.   something missing here;
  417. #endif vipc
  418.  
  419.   /* execvp does not accept an environment arg so the only way
  420.      to pass this environment is to set environ.  Our caller
  421.      is responsible for restoring the ambient value of environ.  */
  422.   environ = env;
  423.   execvp (new_argv[0], new_argv);
  424.  
  425.   write (1, "Couldn't exec the program ", 26);
  426.   write (1, new_argv[0], strlen (new_argv[0]));
  427.   _exit (1);
  428. }
  429.  
  430. init_callproc ()
  431. {
  432.   register char * sh;
  433.   extern char **environ;
  434.   register char **envp;
  435.   Lisp_Object execdir;
  436.  
  437.   /* Turn PATH_EXEC into a path.  Don't look at environment.  */
  438.   Vexec_path = decode_env_path (0, PATH_EXEC);
  439.   Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
  440.   Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
  441.  
  442.   execdir = Fdirectory_file_name (Vexec_directory);
  443.   if (access (XSTRING (execdir)->data, 0) < 0)
  444.     {
  445.       printf ("Warning: executable/documentation dir (%s) does not exist.\012",
  446.           XSTRING (Vexec_directory)->data);
  447.       sleep (2);
  448.     }
  449.  
  450.   sh = (char *) egetenv ("SHELL");
  451.   Vshell_file_name = build_string (sh ? sh : "/bin/sh");
  452.  
  453. #ifndef MAINTAIN_ENVIRONMENT
  454.   /* The equivalent of this operation was done
  455.      in init_environ in environ.c if MAINTAIN_ENVIRONMENT */
  456.   Vprocess_environment = Qnil;
  457. #ifndef CANNOT_DUMP
  458.   if (initialized)
  459. #endif
  460.     for (envp = environ; *envp; envp++)
  461.       Vprocess_environment = Fcons (build_string (*envp),
  462.                     Vprocess_environment);
  463. #endif /* MAINTAIN_ENVIRONMENT */
  464. }
  465.  
  466. syms_of_callproc ()
  467. {
  468.   DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
  469.     "*File name to load inferior shells from.\012\
  470. Initialized from the SHELL environment variable.");
  471.  
  472.   DEFVAR_LISP ("exec-path", &Vexec_path,
  473.     "*List of directories to search programs to run in subprocesses.\012\
  474. Each element is a string (directory name) or nil (try default directory).");
  475.  
  476.   DEFVAR_LISP ("exec-directory", &Vexec_directory,
  477.     "Directory that holds programs that come with GNU Emacs,\012\
  478. intended for Emacs to invoke.");
  479.  
  480. #ifndef MAINTAIN_ENVIRONMENT
  481.   DEFVAR_LISP ("process-environment", &Vprocess_environment,
  482.     "List of strings to append to environment of subprocesses that are started.\012\
  483. Each string should have the format ENVVARNAME=VALUE.");
  484. #endif
  485.  
  486.   defsubr (&Scall_process);
  487.   defsubr (&Scall_process_region);
  488. }
  489.